package judesart.app.client.model;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import judesart.app.domain.Country;

import com.extjs.gxt.ui.client.data.ModelData;

public class CountryModel implements ModelData
{
    private final Country country;

    public CountryModel(Country country)
    {
        this.country = country;
    }

    public Country getCountry()
    {
        return country;
    }

    @Override
    public <X> X get(String property)
    {
        if ("title".equals(property))
        {
            return (X) country.title();
        } else
        {
            return (X) country.name();
        }
    }

    @Override
    public Map<String, Object> getProperties()
    {
        Map<String, Object> p = new HashMap<String, Object>();
        p.put("title", country.title());
        p.put("name", country.name());
        return p;
    }

    @Override
    public Collection<String> getPropertyNames()
    {
        return Arrays.asList("title", "name");
    }

    @Override
    public <X> X remove(String property)
    {
        return (X) get(property);
    }

    @Override
    public <X> X set(String property, X value)
    {
        return (X) get(property);
    }

    static List<CountryModel> models;

    public static List<CountryModel> getCountries()
    {
        if (models == null)
        {
            models = new ArrayList<CountryModel>();
            for (Country country : Country.values())
            {
                models.add(new CountryModel(country));
            }
        }
        return models;
    }

}
